/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.thinktank.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.security.domain.dto.AuthDto;
import com.fzhucc.modules.thinktank.domain.Capital;
import com.fzhucc.modules.thinktank.domain.dto.*;
import com.fzhucc.modules.thinktank.service.CapitalService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Woolsen
 * @date 2022-11-14
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "智库：产业资本管理")
@RequestMapping("/api/capital")
public class CapitalController {

    private final CapitalService capitalService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('capital:list')")
    public void exportCapital(HttpServletResponse response, CapitalQueryCriteria criteria) throws IOException {
        capitalService.download(capitalService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询产业资本")
    @ApiOperation("查询产业资本")
    /*@PreAuthorize("@el.check('capital:list')")*/
    public ResponseEntity<Object> queryCapital(CapitalQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(false);
        return new ResponseEntity<>(capitalService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @AnonymousGetMapping("id/{id}")
    @Log("查询单个产业资本")
    @ApiOperation("查询单个产业资本")
    public CapitalDto queryLeader(@PathVariable("id") Long id) {
        return capitalService.findById(id);
    }

    @AnonymousGetMapping("/show")
    @Log("查询展示的产业资本")
    @ApiOperation("查询展示的产业资本")
    public ResponseEntity<Object> queryCapitalAnon(CapitalQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(true);
        criteria.setIsDelete(false);
        return new ResponseEntity<>(capitalService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增产业资本")
    @ApiOperation("新增产业资本")
    @PreAuthorize("@el.check('capital:add')")
    public ResponseEntity<Object> createCapital(@Validated @RequestBody Capital resources) {
        return new ResponseEntity<>(capitalService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改产业资本")
    @ApiOperation("修改产业资本")
    @PreAuthorize("@el.check('capital:edit')")
    public ResponseEntity<Object> updateCapital(@Validated @RequestBody Capital resources) {
        capitalService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除产业资本")
    @ApiOperation("删除产业资本")
    @PreAuthorize("@el.check('capital:del')")
    public ResponseEntity<Object> deleteCapital(@RequestBody Long[] ids) {
        capitalService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成产业资本绑定小程序码")
    @Log("生成产业资本绑定小程序码")
    @PostMapping(value = "/generateQrCode")
    @PreAuthorize("@el.check('capital:edit')")
    public AllBindDto generateQrCode(@RequestParam Long capitalId) {
        return capitalService.generateQrCode(capitalId);
    }

    @ApiOperation("通过邀请码查询产业资本")
    @Log("通过邀请码查询产业资本")
    @AnonymousPostMapping(value = "/byCode")
    public CapitalDto findByCode(@RequestParam String code) {
        return capitalService.findCapitalByCode(code);
    }

    @ApiOperation("二维码绑定产业资本")
    @Log("二维码绑定产业资本")
    @AnonymousPostMapping(value = "/bind")
    public AuthDto bind(@RequestBody AllBindDto2 dto, HttpServletRequest request) {
        return capitalService.bindCapital(dto, request);
    }

    @Log("根据产业资本专家ListId 查询对应产业资本专家信息")
    @ApiOperation("根据产业资本专家ListId 查询对应产业资本专家信息")
    @PostMapping(value = "/getExpertInfoByListId")
    public ResponseEntity<Object> getExpertInfoByListId(@RequestBody Long[] expertIds) {
        return new ResponseEntity<>(capitalService.getExpertInfoByListId(expertIds),HttpStatus.OK);
    }
}